Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

343
Vistas
JS $.post > dump PHP $_POST data to file

I've been trying this for hours and finally give up. As you can tell I've a huge noob and have little to no idea what I'm doing...

I have some JS being called from a button onclick= which POSTs to a PHP file. I'd like to take this POST data and just write it to a file, nothing fancy, just dumping it raw.

I've tried various methods, but none seem to work - either not writing the data at all, or writing "()", "[]" (if trying to encode the POST data as JSON), or just the word "array" and so on.

Methods I've tried;

file_put_contents('test.txt', file_get_contents('php://input'));  //this I thought *should* definitely work...

var_dump / var_export / print_r

I've tried storing the above as $data and writing that as well. Just nothing I do seems to work at all.

I'm mostly trying to use fopen/write/close to do the deed (because that's all I really "know"). File is writable.

(part of the) JS I'm using to POST:

(from button onclick="send('breakfast'))

function send(food){
if(food == 'breakfast'){
    $.post("recorder.php?Aeggs=" + $("textarea[name=eggs]").val());

I'm not looking to extract(?) values from the POST data, just write it "as-is" to a file, and I'm not bothered on the formatting etc.

Would someone please assist in putting me out of my misery?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could use fopen() and fwrite() to write text to a new file. print_r() could be used to get the structure of the data or you could write the post var itself to the file. But since your client side code is not sending any POST data, use $_GET on the php side instead of $_POST. Here's an example:

$f = fopen("post_log.txt", 'w'); // use 'w' to create the file if not exists or truncate anew if it does exist. See php.net for fopen() on other flags.

fwrite($f, print_r($_GET, true)); // the true on print_r() tells it to return a string

// to write just the Aeggs value to the file, use this code instead of the above fwrite:

fwrite($f, $_GET["Aeggs"]);

fclose($f);

NOTE: The 2nd param to $.post() would contain the "post" data. Since you dont have that in your code, the $_POST on the PHP side will be an empty array.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda